const network_utils = require('../../../http/NetworkUtils.js');
const constants = require('../../../common/Constants.js');
import * as cacheUtils from '../../../model/CacheUtils';
import PageRouter from '../../../utils/PageRouter';

var page = null;
var app = getApp();
let cacheManager = null;
Page({
    data: {
        deviceH: 0,
        deviceW: 0,
        queryRecord: []
    },

    onLoad: function (options) {
        //判断本地是否有搜索记录
        let that = this;
        page = 0;
        cacheManager = new cacheUtils.cacheManager()
            .init('queryMerchants', constants.STORAGE_FUNC.QUERY_MERCHAT);
        let cacheData = cacheManager.getFunc();
        if (cacheData) {
            that.setData({
                queryRecord: cacheData,
            })
        }
    },

    bindKeyInput: function (e) {
        this.setData({
            inputValue: e.detail.value
        })
    },
    btnClick: function (e) {
        this.setData({
            inputValue: this.data.queryRecord[e.currentTarget.dataset.current]
        })

    },

    queryClick: function () {
        if (this.data.inputValue) {
            //缓存查询记录
          this.setData({
            errorMsg: ""
          });
            let that = this;
            that._queryRecordCache();
            let params = {
              agentId:  app.globalData.userData.agentId,
              storeStatus: "3",
              storeName: this.data.inputValue,
              startDate: "",
              endDates: "",
              pageNo: page + 1 + '',
              pageSize: app.globalData.pageSize,
            };
            network_utils.buildRequest(constants.Interfaces.FUNC_SEARCH_STORES, params, {
              doHttpSucess: function (data) {
                if (constants.RetCodes.SUCCESS === data.retCode) {
                 //页面带参数跳转
                        let queryData = JSON.stringify(data["storeList"]);
                        wx.redirectTo({
                          url: './yesterdayTradQuery/yesterdayTradQuery?queryData=' + queryData + '&content=' + that.data.inputValue
                        });
                        return true;
                }

                return false;
              },
            }).send();
            // let params = {
            //     content: this.data.inputValue,
            //     pageNo: "1",
            //     pageSize: app.globalData.pageSize,
            // };
            // network_utils.buildRequest(constants.Interfaces.FUNC_Merchant_Query, params, {
            //     doHttpSucess: function (data) {
            //         if (constants.RetCodes.SUCCESS === data.retCode) {
            //             //页面带参数跳转
            //             let queryData = JSON.stringify(data["storeList"]);
            //             wx.redirectTo({
            //               url: './yesterdayTradQuery/yesterdayTradQuery?queryData=' + queryData + '&content=' + that.data.inputValue
            //             });
            //             return true;
            //         }
            //         return false;
            //     }
            // }).send();
        }else{
            PageRouter.wxNavigateTo("queryMerchants", '../yesterdayTrad/yesterdayTrad', true);
          // wx.redirectTo({
          //   url: '../yesterdayTrad/yesterdayTrad'
          // });
        }
    },
    _queryRecordCache: function () {
        let that = this;
        let queryRecord1 = that.data.queryRecord;
        //查询本地存储与搜索是否相同
        for (var i = 0; i < queryRecord1.length; i++) {
            if (queryRecord1[i] == that.data.inputValue) {
                //删除
                queryRecord1.splice(i, 1)
            }
        }
        if (queryRecord1.length == 9) {
            queryRecord1.pop();
        }
        queryRecord1.splice(0, 0, that.data.inputValue);

        that.setData({
            queryRecord: queryRecord1
        })
        cacheManager.cacheFunc(that.data.queryRecord);
    }

});
